package com.jdwx.opengl.kotlin2

class VariableVSValue {
    fun declareVar() {
        var a = 1
        a = 2
        println(a)
        println (a::class)
        println (a::class.java)
        var x = 5 // 自动推断出 `Int` 类型 x += 1
        println("x = $x")
    }

    fun declareVal() {
        val b = "a"
        //b = "b" //编译器会报错: Val cannot be reassigned println(b)
        println(b::class)
        println(b::class.java)
        val c:Int=1 //立即赋值
        val d = 2 // 自动推断出 `Int` 类型
        val e: Int // 如果没有初始值类型不能省略 e = 3 // 明确赋值
        e = 3
        println("c = $c, d = $d, e = $e")
    }

    fun typeInference(){
        val str = "abc"
        println(str)
        println(str is String)
        println(str::class)
        println(str::class.java)
    }
    fun getLength(obj:Any):Int?{
        var result = 0
        if( obj is String){
            println( obj::class )
            result = obj.length
            println( "result:($result)")
        }
        println( obj::class )
        return result
    }
}
fun main(){
    var va = VariableVSValue();
    va.declareVal()
    va.declareVar()
    va.typeInference()
    var x:Int = 10
    var y:Long = x.toLong()

    if( x is Int ){
        println( Int::class )
    }

    va.getLength( "xiaobai" )


    val rawString = """
fun helloWorld(val name : String) { println("Hello, world!")
} """
        println(rawString)

    var max2:Int
    var a = 10
    var b = 1
    var new = if( a>b ){
        max2 = a
        11
    }else {
        max2 = b
        100
    }
    println("max2=$max2,new=$new")

    if( true ) println("zhen")

    when( b ){
        1-> {
            println("1:")
            println("1")
        }
        else->{
            println( "非" )
        }
    }

    cases(1)
    var validNumbers = arrayOf(1,2,3)
    for (i in validNumbers){


        println("i=$i")
    }

    fun max( a:Int, b:Int ) = if( a> b ) a else b

    print( max(10,20) )
    returnDemo_2()
    returnDemo4()
}

fun cases(obj: Any){
    var validNumbers = arrayOf(1,2,3)
    when( obj ){
        1,2-> print("1")
        "hello"-> print("hello")
        is String-> print("is string")
        in 1..10-> print("在1-10之间")
        !in validNumbers -> print("validNumbers中存在此值")
        else-> print("else")
    }

}
fun returnFun( a:Int, b:Int ):Int{
    if( a> b ) return a else return b
}
fun returnDemo_2() {
    println(" START " + ::returnDemo_2.name)
    val intArray = intArrayOf(1, 2, 3, 4, 5)
    intArray.forEach(fun(a: Int) {
        if (a == 3) return
        println(a)
    })
    println(" END " + ::returnDemo_2.name)
}
fun returnDemo4() {
    println(" START " + ::returnDemo4.name)
    val intArray = intArrayOf(1, 2, 3, 4, 5)
    intArray.forEach here@ {
        if (it == 3) return@here // 指令跳转到 lambda 表达式标签 here@ 处。继续下一个it=4 的遍历循环
        println(it)
    }
    println(" END " + ::returnDemo4.name)
    intArray.forEach  {
        if (it == 3) return@forEach // 指令跳转到 lambda 表达式标签 here@ 处。继续下一个it=4 的遍历循环
        println(it)
    }
    println(" END " + ::returnDemo4.name)

}